package DataAnalysis_qcl

import org.apache.spark.sql.SparkSession

import java.util.Properties

object DataAnalysis03 {
  def main(args: Array[String]): Unit = {
    /*
        DataAnalysis03:分析既有电梯又靠近地铁的房源信息在每个区的分布
     */
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("数据分析03")
      .enableHiveSupport()
      .getOrCreate()

    val mysql_connect=new Properties()
    mysql_connect.setProperty("user","root")
    mysql_connect.setProperty("password","123456")
    mysql_connect.setProperty("driver","com.mysql.jdbc.Driver")

    spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/qcl?useSSL=false","data_processing",mysql_connect)
      .createOrReplaceTempView("data")

//  not:取反
    spark.sql("use qcl")
   val result= spark.sql(
      """
        |select
        |t1.downtown,
        |count(*) as number
        |from(
        |select * from data where elevator=1 and not(subway="0")
        |) as t1
        |group by downtown
        |order by number desc
        |""".stripMargin)


    result.write.mode("overwrite")
      .jdbc("jdbc:mysql://192.168.40.110:3306/qcl?useSSL=false", "data_analysis03", mysql_connect)

    //  将结果存入hdfs
    result.repartition(1).write.mode("overwrite")
      .csv("/qcl/data_analysis/data_analysis03.csv")


    spark.close()

  }

}
